package com.waimai.mobile.ui.fragment;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.waimai.mobile.R;
import com.waimai.mobile.ui.WaiMaiApp;
import com.waimai.mobile.ui.activity.AddressListActivity_;
import com.waimai.mobile.ui.activity.LoginActivity_;
import com.waimai.mobile.ui.activity.UserDetailActivity_;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;


/**
 * Created by lin on 2015/5/29.
 */

@EFragment(R.layout.fragment_user_center)
public class UserCenterFragment extends Fragment {

    @App
    public WaiMaiApp app;

    @ViewById
    public LinearLayout notLoginContainerLL;

    @ViewById(R.id.toLoginBtn)
    public Button toLoginBtn;

    @ViewById
    public RelativeLayout loginUserContainerRL;

    @ViewById
    public RelativeLayout toAddressListContainerRl;

    @ViewById(R.id.userNameTv)
    public TextView userNameTv;

    @ViewById(R.id.phoneTv)
    public TextView phoneTv;

    @ViewById(R.id.balanceTv)
    public TextView balanceTv;


    @AfterViews
    public void afterViews(){

    }

    @Click(R.id.toLoginBtn)
    public void toLoginBtnClick(View view){
        Intent intent = new Intent(getActivity(), LoginActivity_.class);
        startActivity(intent);
    }

    @Click(R.id.loginUserContainerRL)
    public void loginUserContainerRLClick(View view){
        Intent intent = new Intent(getActivity(), UserDetailActivity_.class);
        startActivity(intent);
    }

    @Click(R.id.toAddressListContainerRl)
    public void toAddressListContainerRlClick(View view){
        if(app.getUser() != null){
            Intent intent = new Intent(getActivity(), AddressListActivity_.class);
            startActivity(intent);
        }else{
            Toast.makeText(getActivity(), "请先登录", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if(app.getUser() != null){
            notLoginContainerLL.setVisibility(View.GONE);
            loginUserContainerRL.setVisibility(View.VISIBLE);
            userNameTv.setText(app.getUser().getUserName());
            phoneTv.setText(app.getUser().getPhone());
            balanceTv.setText("￥" + app.getUser().getBalance()+"");
        }else{
            notLoginContainerLL.setVisibility(View.VISIBLE);
            loginUserContainerRL.setVisibility(View.GONE);
        }
    }
}